package net.seehope.foodie.controller;

import java.lang.reflect.InvocationTargetException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.seehope.foodie.common.JsonResult;
import net.seehope.foodie.service.IndexService;


@Api("渲染商城首页的控制器")
@RestController
@RequestMapping("/index")
public class IndexController {
	
	@Autowired
	private IndexService indexService;
	
	
	
	
	@ApiOperation("渲染首页的轮播图")
	@GetMapping("/carousel")
	public JsonResult renderCarousel() throws IllegalAccessException, InvocationTargetException {
		return JsonResult.isOk(indexService.renderCarousel());
	}
	
	
	
	
	@ApiOperation("渲染首页的商品分类加载")
	@GetMapping("/cats")
	public JsonResult renderCategory() {
		return JsonResult.isOk(indexService.renderCategory());
	}
	
	//{rootCatId:\\d+}是一个占位符，取前端传进来的参数
	@ApiOperation("渲染首页分类目录加载，异步请求")
	@GetMapping("/subCat/{rootCatId:\\d+}")
	public JsonResult renderSubCat(@PathVariable String rootCatId) {
		return JsonResult.isOk(indexService.renderSubCategory(rootCatId));
	}
	
	
	@ApiOperation("渲染首页分类中商品列表，懒加载")
	@GetMapping("/sixNewItems/{rootCatId:\\d+}")
	public JsonResult renderIndexInfo(@PathVariable String rootCatId) {
		return JsonResult.isOk(indexService.renderIndexInfo(rootCatId));
	}
	
	
}
